package logic

import (
	"context"
	"strconv"
	"strings"

	"mall/common/globalkey"
	"mall/service/attachment/rpc/attachmentclient"
	"mall/service/backend/api/internal/logic/utils/parse"
	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/utils/pointy"
	"mall/service/blog/rpc/blogclient"
	"mall/service/forum/rpc/forumclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type TmpDiagnoseLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 批量同步七牛云
func NewTmpTmpDiagnoseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TmpDiagnoseLogic {
	return &TmpDiagnoseLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TmpDiagnoseLogic) TmpDiagnose(req *types.BackendDiagnoseReq) (*types.BackendDiagnoseResp, error) {
	logx.Infof("DiagnosePost BackendDiagnosePostsReq :%+v", req)
	var count int
	var err error
	switch req.ObjectType {
	//mall_forum
	case uint32(globalkey.AttachmentLogTypeForumPost), uint32(globalkey.AttachmentLogTypeForumTopic):
		count, err = l.diagnoseForum(req)
	//mall_blog
	case uint32(globalkey.AttachmentLogTypePost), uint32(globalkey.AttachmentLogTypeKnowledgeBase),
		uint32(globalkey.AttachmentLogTypePage), uint32(globalkey.AttachmentLogTypeTerm):
		count, err = l.diagnoseBlog(req)
	}

	if err != nil {
		return nil, err
	}

	return &types.BackendDiagnoseResp{
		Total: int64(count),
	}, nil

}
func (l *TmpDiagnoseLogic) diagnoseForum(req *types.BackendDiagnoseReq) (count int, err error) {
	page, pageSize := parse.ParsePageInfo(
		pointy.GetPointer(req.Current),
		pointy.GetPointer(req.PageSize),
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)
	res, err := l.svcCtx.ForumRpc.BackendGetAttachmentList(l.ctx, &forumclient.BackendAttachmentListReq{
		Page:       page,
		PageSize:   pageSize,
		ObjectType: req.ObjectType,
	})
	if err != nil {
		return 0, err
	}
	if len(res.DiagnoseAttachmentsIds) == 0 {
		return 0, errorx.NewInvalidArgumentError("cms.diagnoseForumAttachmentIdsEntry")
	}
	var ids []string
	for _, v := range res.DiagnoseAttachmentsIds {
		ids = append(ids, strconv.FormatUint(v, 10))
	}
	attRes, err := l.svcCtx.AttachmentRpc.BackendGetFileList(l.ctx, &attachmentclient.FileListReq{
		Ids: pointy.GetPointer(strings.Join(ids, ",")),
	})
	if err != nil {
		return 0, err
	}
	if len(attRes.List) > 0 {
		var attMap = make(map[uint64]string)
		for _, att := range attRes.List {
			if att.Url != nil && len(strings.TrimSpace(*att.Url)) > 0 {
				attId, err := strconv.ParseUint(*att.Id, 10, 64)
				if err != nil {
					return 0, err
				}
				attMap[attId] = "https://static.scentrobe.com" + *att.Url
			}
		}
		if len(attMap) > 0 {
			_, err := l.svcCtx.ForumRpc.BatchSyncQiniu(l.ctx, &forumclient.BatchSyncQiniuReq{
				SyncType: uint64(req.ObjectType),
				Url:      attMap,
			})
			if err != nil {
				return 0, err
			}
		}
	}
	return 0, nil
}

func (l *TmpDiagnoseLogic) diagnoseBlog(req *types.BackendDiagnoseReq) (count int, err error) {
	page, pageSize := parse.ParsePageInfo(
		pointy.GetPointer(req.Current),
		pointy.GetPointer(req.PageSize),
		l.svcCtx.Config.Query.DefaultAmount,
		l.svcCtx.Config.Query.MaxAmount,
	)
	res, err := l.svcCtx.BlogRpc.BackendGetAttachmentList(l.ctx, &blogclient.BackendAttachmentListReq{
		Page:       page,
		PageSize:   pageSize,
		ObjectType: req.ObjectType,
	})
	if err != nil {
		return 0, err
	}
	if len(res.DiagnoseAttachmentsIds) == 0 {
		return 0, errorx.NewInvalidArgumentError("cms.diagnoseBlogAttachmentIdsEntry")
	}
	var ids []string
	for _, v := range res.DiagnoseAttachmentsIds {
		ids = append(ids, strconv.FormatUint(v, 10))
	}
	attRes, err := l.svcCtx.AttachmentRpc.BackendGetFileList(l.ctx, &attachmentclient.FileListReq{
		Ids: pointy.GetPointer(strings.Join(ids, ",")),
	})
	if err != nil {
		return 0, err
	}
	if len(attRes.List) > 0 {
		var attMap = make(map[uint64]string)
		for _, att := range attRes.List {
			if att.Url != nil && len(strings.TrimSpace(*att.Url)) > 0 {
				attId, err := strconv.ParseUint(*att.Id, 10, 64)
				if err != nil {
					return 0, err
				}
				attMap[attId] = "https://static.scentrobe.com" + *att.Url
			}
		}
		if len(attMap) > 0 {
			_, err := l.svcCtx.BlogRpc.BatchSyncQiniu(l.ctx, &blogclient.BatchSyncQiniuReq{
				SyncType: uint64(req.ObjectType),
				Url:      attMap,
			})
			if err != nil {
				return 0, err
			}
		}
	}
	return 0, nil
}
